﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Utility
{
   public class FileUtil
    {
        public class fileinfoModel
        {
            public string name { get; set; }
            public long size { get; set; }
            public DateTime createtime { get; set; }
            public string file { get; set; }
        }

        /// <summary>
        /// 遍历目录下文件，子文件夹
        /// </summary>
        /// <param name="sSourcePath">相对路径  wwwroot//upload</param>
        /// <returns></returns>
        public static List<fileinfoModel> FindFile(string sSourcePath)
        {
            List<fileinfoModel> list = new List<fileinfoModel>();
            //遍历文件夹
            System.IO.DirectoryInfo theFolder = new System.IO.DirectoryInfo(sSourcePath);
            System.IO.FileInfo[] thefileInfo = theFolder.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);
            foreach (System.IO.FileInfo NextFile in thefileInfo)  //遍历文件
            {
                fileinfoModel model = new fileinfoModel
                {
                    name= NextFile.Name,
                    size = NextFile.Length,
                    createtime = NextFile.LastWriteTime,
                    file = NextFile.FullName
                };
                list.Add(model);
            }
                
            //遍历子文件夹
            System.IO.DirectoryInfo[] dirInfo = theFolder.GetDirectories();
            foreach (System.IO.DirectoryInfo NextFolder in dirInfo)
            {
                //list.Add(NextFolder.ToString());
                System.IO.FileInfo[] fileInfo = NextFolder.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                foreach (System.IO.FileInfo NextFile in fileInfo)  //遍历文件
                {
                    fileinfoModel model = new fileinfoModel 
                    {
                        name = NextFile.Name,
                        size = NextFile.Length,
                        createtime = NextFile.LastWriteTime,
                        file = NextFile.FullName
                    };
                    list.Add(model);
                }
            }
            return list;
        }

        
    }
}
